package me.shurufa.widget;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.w3c.dom.Text;

import me.shurufa.R;
import me.shurufa.utils.Utils;

/**
 * 作者 : andy
 * 日期 : 16/3/21 15:34
 * 邮箱 : andyxialm@gmail.com
 * 描述 : 我的评论 我的收藏 顶部Title & Search Bar
 */
public class HeaderWithSearchBar extends LinearLayout implements View.OnClickListener, TextWatcher {

    private TextView mTitleTv;
    private EditText mSearchEt;
    private TextView mSearchTv;
    private ImageView mSearchIv;
    private LinearLayout mSearchLayout;
    private FrameLayout mSearchParentLayout;

    private OnSearchListener mOnSearchListener;
    private String mBeforeContent;


    public HeaderWithSearchBar(Context context) {
        this(context, null);
    }

    public HeaderWithSearchBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HeaderWithSearchBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public HeaderWithSearchBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        setOrientation(HORIZONTAL);
        View contentView = View.inflate(getContext(), R.layout.layout_header_with_search, null);
        mTitleTv = (TextView) contentView.findViewById(R.id.tv_header_title);
        mSearchEt = (EditText) contentView.findViewById(R.id.et_search);
        mSearchLayout = (LinearLayout) contentView.findViewById(R.id.ll_search);
        mSearchParentLayout = (FrameLayout) contentView.findViewById(R.id.fl_search);
        mSearchIv = (ImageView) contentView.findViewById(R.id.iv_search);
        mSearchTv = (TextView) contentView.findViewById(R.id.tv_search);

        mTitleTv.setOnClickListener(this);
        mSearchTv.setOnClickListener(this);
        mSearchLayout.setOnClickListener(this);
        mSearchEt.addTextChangedListener(this);
        mSearchEt.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                // 若失去焦点
                if (!hasFocus) {
                    mSearchEt.setVisibility(GONE);
                    mSearchTv.setVisibility(VISIBLE);
                } else {
                    mSearchEt.setVisibility(VISIBLE);
                    mSearchTv.setVisibility(GONE);
                }
            }
        });
        // 编辑框回车搜索
        mSearchEt.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
                    // 触发-搜索
                    handleSearch();
                    // 防止-焦点下移
                    return true;
                }

                return false;
            }
        });
        addView(contentView);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_search:
                if (mSearchEt.getVisibility() != GONE) {
                    handleSearch();
                    break;
                }
            case R.id.ll_search:
                mSearchEt.setVisibility(VISIBLE);
                // mSearchLayout.setVisibility(GONE);
                mSearchIv.setVisibility(GONE);
                mSearchEt.requestFocus();
                Utils.showSoftkeyboard(getContext(), mSearchEt);
                break;
        }
    }

    private void handleSearch() {
        /* mSearchEt.setVisibility(mSearchEt.getVisibility() == VISIBLE ? GONE : VISIBLE);
        mSearchIv.setVisibility(VISIBLE);

        if (TextUtils.isEmpty(mSearchEt.getText().toString()) && mSearchEt.getVisibility() != GONE) {
            return;
        }*/
        if (mOnSearchListener != null) {
            mOnSearchListener.onSearch(mSearchEt.getText().toString());
        }
        // mSearchEt.setText("");
        Utils.closeInputMethod(getContext(), mSearchEt);
    }

    /**
     * 隐藏搜索框
     */
    public void hideSearch() {
        mSearchParentLayout.setVisibility(INVISIBLE);
        mSearchParentLayout.setClickable(false);
    }

    /**
     * 重置
     */
    public void reset() {
        mSearchEt.setVisibility(GONE);
        mSearchIv.setVisibility(VISIBLE);

        if (TextUtils.isEmpty(mSearchEt.getText().toString()) && mSearchEt.getVisibility() != GONE) {
            return;
        }
        mSearchEt.setText("");
        Utils.closeInputMethod(getContext(), mSearchEt);
    }

    public EditText getEditText() {
        return mSearchEt;
    }

    public void setOnSearchListener(OnSearchListener l) {
        this.mOnSearchListener = l;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        mBeforeContent = s.toString();
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        // 之前不为空  之后为空
        if (!TextUtils.isEmpty(mBeforeContent) && TextUtils.isEmpty(s.toString())) {
            if (null != mOnSearchListener) {
                mOnSearchListener.onBack();
            }
            // 重置
            mBeforeContent = "";
            // 隐藏键盘
            Utils.closeInputMethod(getContext(), mSearchEt);
            mSearchEt.clearFocus();
            mSearchIv.setVisibility(VISIBLE);
        }
    }

    public interface OnSearchListener {
        /**
         * 查询
         * @param content
         */
        void onSearch(String content);

        /**
         * 查询后 删除所有 知道EditText清空时，拉取所有列表
         */
        void onBack();
    }

}
